package org.magicbox.qjunTPO.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.magicbox.qjunTPO.common.lang.Result;
import org.magicbox.qjunTPO.entity.Comments;
import org.magicbox.qjunTPO.entity.Poems;
import org.magicbox.qjunTPO.service.CommentsService;
import org.magicbox.qjunTPO.service.PoemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 欢迎添加科技实践队粉丝群：690849711
 * @since 2021-11-16
 */
@RestController
@RequestMapping("/comments")
public class CommentsController {
    @Autowired
    CommentsService comService;


    @Autowired
    PoemsController poemsController;

    @Autowired
    UserController userController;

    /**
     * 添加评论
     * @param comment 评论实体
     * @return 是否成功
     */
    @RequestMapping("/add")
    public Result addComment(@Validated @RequestBody Comments comment){
        Integer uid = null;//用于存储这个条评论评论的被评论用户id
        boolean uidok = false;

        LocalDateTime createdate = LocalDateTime.now(); //当前时间 , 对于时间 之后不再注释
        comment.setCommentCreate(createdate);

        //当评论无前驱的时候 回复的就是该poem 的用户
        if(comment.getPrecursors() == null) {
            uid = poemsController.byIdGetUid(comment.getPoemId());//获取poem的作者id
            uidok = true;
        }
        else {
            //当评论有前驱的时候 回复的是前驱评论的用户
            Integer exchenge = comService.getById(comment.getPrecursors())
                    .getUid();      //现在所回复的人的id
            uidok = !(exchenge.equals(comment.getUid()));
            if(uidok)
                uid = exchenge;

        }

        comment.setCommentuser(uid);//设置回复的用户
        boolean saveok = comService.save(comment);

        //当评论的时候需要给给poem评论+1,当评论有前驱的时候,需要给poem和前驱评论回复+1
        poemsController.requestadd(comment.getPoemId());    //调用评论+1

        if(saveok & uidok) userController.upNewRequest(uid);
        return Result.success("评论成功!");
    }

    /**
     * 删除评论
     * @param comment 需要给实体传入两个参数,一个是评论id 一个是用户的id,唯有两者id匹配才可以删除
     * @return      返回删除结果
     */
    @RequestMapping("/delet")
    public Result deletComment(@Validated @RequestBody Comments comment){
        Comments commentForId = comService.getById(comment.getCommentId());
        Assert.notNull(commentForId,"评论不存在!");
        if(!comment.getUid().equals(commentForId.getUid())){
            return Result.fail(203,"无权限!");
        }
        try {
            comService.removeById(comment.getCommentId());
        }catch (Exception e){
            Result.fail(200,"操作失败");
        }
        return Result.success("成功删除");
    }

    /**
     * 通过id获取用户的评论
     * @param uid   用户id
     * @param pageNumber    页码
     * @return      40条用户的评论
     */
    @RequestMapping("/geUserComments/{uid}/{pageNumber}")
    public Result geUserComments(@PathVariable("uid")Integer uid, @PathVariable("pageNumber")Integer pageNumber){
        Page page = new Page(pageNumber,40);
        Map<String,Integer> eqMap = new HashMap<>();
        eqMap.put("uid",uid);
        IPage pageData = comService.page(page,new QueryWrapper<Comments>().allEq(eqMap).orderByDesc("comment_create"));
        return Result.success(pageData);
    }
}
